package net.xdclass.util;

import cn.hutool.json.JSONObject;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

/**
 * @author whg
 * @date 2025-05-15
 * @description
 */
public class ImageToBase64 {

    public static final String name = "浙江-123";

    public static String convertImageToBase64(String imagePath) throws IOException {
        Path path = Paths.get(imagePath);
        byte[] imageBytes = Files.readAllBytes(path);
        return Base64.getEncoder().encodeToString(imageBytes);
    }

    public static void main(String[] args) {
        try {
            //1、影像
            //String data = convertImageToBase64("D:\\home\\image\\"+ name +".png");
            //2、checkId
            //String data = "1892109698";
            //3、五要素
            //云南-五要素
            /*Map<String,String> map = new HashMap<>();
            map.put("receiptNo","25532000000044006875");
            map.put("receiptCode","");
            map.put("receiptCheckCode","");
            map.put("totalAmount","2416.71");
            map.put("invoiceDate","2025-04-18");
            String data = new JSONObject(map).toString();*/
            //北京-五要素
            /*Map<String,String> map = new HashMap<>();
            map.put("receiptNo","24112000000014204881");
            map.put("receiptCode","");
            map.put("receiptCheckCode","");
            map.put("totalAmount","498");
            map.put("invoiceDate","2024-03-26");*/
            //辽宁-五要素
            /*Map<String,String> map = new HashMap<>();
            map.put("receiptNo","01569993");
            map.put("receiptCode","021002300304");
            map.put("receiptCheckCode","365806");
            map.put("totalAmount","8");
            map.put("invoiceDate","2025-01-13");*/
            //广东1-五要素
            /*Map<String,String> map = new HashMap<>();
            map.put("receiptNo","0003789907");
            map.put("receiptCode","44060224");
            map.put("receiptCheckCode","22be59");
            map.put("totalAmount","51056.2");
            map.put("invoiceDate","2025-02-02");*/
            //广东2-五要素
            /*Map<String,String> map = new HashMap<>();
            map.put("receiptNo","0003790070");
            map.put("receiptCode","44060224");
            map.put("receiptCheckCode","09dd4f");
            map.put("totalAmount","9595.09");
            map.put("invoiceDate","2025-02-11");*/
            //江苏-五要素
            /*Map<String,String> map = new HashMap<>();
            map.put("receiptNo","25322000000185233443");
            map.put("receiptCode","");
            map.put("receiptCheckCode","");
            map.put("totalAmount","740.62");
            map.put("invoiceDate","2025-04-24");*/
            //海南-五要素
            /*Map<String,String> map = new HashMap<>();
            map.put("receiptNo","0077156788");
            map.put("receiptCode","46060124");
            map.put("receiptCheckCode","dcc132");
            map.put("totalAmount","260");
            map.put("invoiceDate","2024-12-25");*/
            //广西-增值税电子发票-五要素
            /*Map<String,String> map = new HashMap<>();
            map.put("receiptNo","34095686");
            map.put("receiptCode","045002100111");
            map.put("receiptCheckCode","738012");
            map.put("totalAmount","6888.10");
            map.put("invoiceDate","2025-02-22");*/
            //浙江-其他电子发票--五要素
            /*Map<String,String> map = new HashMap<>();
            map.put("receiptNo","64024642");
            map.put("receiptCode","233002500111");
            map.put("receiptCheckCode","945203");
            map.put("totalAmount","50");
            map.put("invoiceDate","2025-03-26");*/
            //浙江-医疗电子票--五要素
            /*Map<String,String> map = new HashMap<>();
            map.put("receiptNo","0920285912");
            map.put("receiptCode","33060125");
            map.put("receiptCheckCode","x5jgyr");
            map.put("totalAmount","508.24");
            map.put("invoiceDate","2025-03-22");*/
            //重庆-医疗电子票--五要素
            Map<String,String> map = new HashMap<>();
            map.put("receiptNo","0064739886");
            map.put("receiptCode","50060125");
            map.put("receiptCheckCode","e2c52d");
            map.put("totalAmount","164.69");
            map.put("invoiceDate","2025-05-13");
            //江苏-全电票--三要素
            /*Map<String,String> map = new HashMap<>();
            map.put("receiptNo","25332000000200953290");
            map.put("receiptCode","");
            map.put("receiptCheckCode","");
            map.put("totalAmount","3000");
            map.put("invoiceDate","2025-05-16");*/
            //江苏-税票--五要素
            /*Map<String,String> map = new HashMap<>();
            map.put("receiptNo","00350022");
            map.put("receiptCode","033022300104");
            map.put("receiptCheckCode","551191");
            map.put("totalAmount","312.18");
            map.put("invoiceDate","2024-12-07");*/
            //test-税票--五要素
            /*Map<String,String> map = new HashMap<>();
            map.put("receiptNo","0111381052");
            map.put("receiptCode","31060122");
            map.put("receiptCheckCode","81ab39");
            map.put("totalAmount","538.72");
            map.put("invoiceDate","2022-06-08");*/
            /*Map<String,String> map = new HashMap<>();
            map.put("receiptNo","0473587182");
            map.put("receiptCode","33060125");
            map.put("receiptCheckCode","106734");
            map.put("totalAmount","25.00");
            map.put("invoiceDate","2025-03-24");
            map.put("detailSuccess","false");*/
            String data = new JSONObject(map).toString();
            generateSign(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void generateSign(String data){
        //String noise = UUID.randomUUID().toString().replace("-", "");
        String noise = "35f5978009c14a54a6647af44a3b4881";
        //String appId = "MMztV8CwbJV2YL5J";
        String appId = "A9HZ30bcM2D242Ce";
        String signCheck = "appid=" + appId +"&data="+data+"&noise="+noise;
        String md5Sign = MD5.md5_32(signCheck).toUpperCase();
        Map<String,String> map = new HashMap<>();
        map.put("sign",md5Sign);
        map.put("data",data);
        map.put("noise",noise);
        map.put("source","bosi");
        JSONObject jsonObject = new JSONObject(map);
        System.out.println(jsonObject);
        // 指定文件路径
        String filePath = "D:\\home\\json\\"+ name +".txt";

        /*File file = new File(filePath);

        // 如果文件不存在，则创建文件
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                System.out.println("文件创建失败: " + e.getMessage());
                return;
            }
        }

        // 将 JSONObject 写入文件
        try (FileWriter fileWriter = new FileWriter(filePath)) {
            fileWriter.write(jsonObject.toString());
            System.out.println("JSONObject 已成功写入到文件: " + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }
}
